﻿using System;

/*
 * Write a program for extracting all email addresses from given text.
 * All substrings that match the format <identifier>@<host>…<domain> should be recognized as emails.
 */

namespace EmailValidation
{
    class EmailValidation
    {
        static bool IsName(string name)
        {
            bool isFine = true;

            if (name[name.Length - 1] == '.')
            {
                isFine = false;
            }

            if (char.IsDigit(name[0]))
            {
                isFine = false;
                foreach (var ch in name)
                {
                    if (char.IsLetter(ch) || ch == '_')
                    {
                        isFine = true;
                    }
                }
            }

            return isFine;
        }

        static bool IsDomain(string domain)
        {
            bool isFine = true;

            if (domain.Length < 2 || domain.Length > 5)
            {
                isFine = false;
            }

            foreach (char ch in domain)
            {
                if (!char.IsLetter(ch))
                {
                    isFine = false;
                }
            }

            return isFine;
        }

        static bool IsHost(string host)
        {
            if (host.Length < 2)
            {
                return false;
            }

            if (!char.IsLetterOrDigit(host[0]) || !char.IsLetterOrDigit(host[host.Length - 1]))
            {
                return false;
            }

            if (host.Contains(".."))
            {
                return false;
            }

            return true;
        }

        static bool CheckEmail(string mail)
        {
            if (mail.Length < 7 || !char.IsLetterOrDigit(mail[0]) || mail.IndexOf('@') < 1 || mail.IndexOf('.') < 1)
            {
                return false;
            }
            int startKliomba = mail.IndexOf('@');

            if (startKliomba != mail.LastIndexOf('@'))
            {
                return false;
            }

            string domainAndHost = mail.Substring(startKliomba + 1);

            if (!domainAndHost.Contains(".") || domainAndHost[domainAndHost.Length - 1] == '.')
            {
                return false;
            }

            bool name = IsName(mail.Substring(0, startKliomba));
            bool domain = IsDomain(domainAndHost.Substring(domainAndHost.LastIndexOf('.') + 1));
            bool host = IsHost(domainAndHost.Substring(0, domainAndHost.LastIndexOf('.')));

            return name && domain && host;
        }

        static void Main()
        {
            string text = "pesho@yahoo.com 8@a@.com.com 8gosho@mail.bg 88@mail.bg pesho@.mail.bg";
            string[] emails = text.Split(' ');
            foreach (string mail in emails)
            {
                Console.WriteLine("{0} => {1}", mail,CheckEmail(mail));
            }
        }
    }
}
